package chapter6.meine;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JApplet;
import javax.swing.JPanel;

public class SimpleRandomColoredText extends JApplet{
	
	
	private class DrawText extends JPanel {
		private DrawText (String message){
			this.message = message;
		    setBackground(Color.BLACK);
		}

		private String message;

		private Font font1, font2, font3, font4;

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			int fontSize = 0;
			int fontStyle = 0;
			int fontNumber = 0;

			
			int height = getHeight();
			int width = getWidth();

			for (int i = 0; i < 25; i++) {
				font1 = new Font("SansSerif", fontStyle, fontSize);
				font2 = new Font("Serif", fontStyle, fontSize);
				font3 = new Font("Monospace", fontStyle, fontSize);
				font4 = new Font("Dialog", fontStyle, fontSize);
				int x,y;
				 x = -50 + (int) (Math.random() * (width + 40));
				 y = -20 + (int) (Math.random() * (height + 20));

				fontStyle = (int) (Math.random() * 5);
				fontSize = 20 + (int) (Math.random() * 30);
				fontNumber = 1 + (int) (Math.random() * 4);

				switch (fontNumber) {
				case 1:
					g.setFont(font1);
					break;
				case 2:
					g.setFont(font2);
					break;
				case 3:
					g.setFont(font3);
					break;
				case 4:
					g.setFont(font4);
					break;
				default:
					break;
				}
		           float hue = (float)Math.random();
		           g.setColor( Color.getHSBColor(hue, 1.0F, 1.0F) );

				g.drawString(message, x, y);

			} // end of for loop
		}//end of paintComponent() method

	}//end of class DrawText
	
	   public void init() {
		    
		     DrawText content = new DrawText("I've Done IT!");
		     setContentPane(content);
		     addMouseListener(new Repaint());
		   }
	   public class Repaint implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
	         Component source = (Component)e.getSource();
	         source.repaint();
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		   
	   }
	   
	   

}// end of SimpleRandomColoredText applet
